package sfs

// 包头格式

type PacketHeader struct {
	expectedLen int
	binMod      bool
	compressed  bool
	encrypted   bool
	blueBoxed   bool
	bigSized    bool
}

func NewPacketHeader(encrypted, compressed, blueBoxed, bigSized bool) PacketHeader {
	return PacketHeader{
		expectedLen: -1,
		binMod:      true,
		compressed:  compressed,
		encrypted:   encrypted,
		blueBoxed:   blueBoxed,
		bigSized:    bigSized,
	}
}

func NewPacketHeaderFromBinary(headerByte int) PacketHeader {
	return NewPacketHeader((headerByte&0x40) > 0, (headerByte&0x20) > 0, (headerByte&0x10) > 0, (headerByte&0x8) > 0)
}

func (p PacketHeader) ExceptedLen() int {
	return p.expectedLen
}

func (p PacketHeader) BinMod() bool {
	return p.binMod
}

func (p PacketHeader) Compressed() bool {
	return p.compressed
}

func (p PacketHeader) Encrypted() bool {
	return p.encrypted
}

func (p PacketHeader) BlueBoxed() bool {
	return p.blueBoxed
}

func (p PacketHeader) BigSized() bool {
	return p.bigSized
}

func (p PacketHeader) Encode() int {
	headerByte := 0

	if p.binMod {
		headerByte += 0x80
	}

	if p.encrypted {
		headerByte += 0x40
	}

	if p.compressed {
		headerByte += 0x20
	}

	if p.blueBoxed {
		headerByte += 0x10
	}

	if p.bigSized {
		headerByte += 0x08
	}

	return headerByte
}
